package com.luck.emp.storage.web;


import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

import com.luck.emp.entity.EmpTCgM;
import com.luck.emp.entity.EmpTWarehouse;
import com.luck.emp.entity.EmpTWhExM;
import com.luck.emp.storage.logic.IStorageLogic;
import com.luck.emp.storage.qry.StorageQry;
import com.luck.emp.storage.vo.StorageCheck;
import com.luck.emp.storage.vo.StorageCheckDetail;
import com.luck.emp.wh.logic.IWhLogic;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.action.BaseActionSupport;
import com.luck.pf.util.DateUtil;
import com.luck.pf.util.ObjectUtil;
import com.luck.pf.util.Page;

/**
 * 说明:ACTION类
 * Copyright: Copyright (c) 2011
 * 深圳**科技
 * @author 
 * 
 */
public class StorageAction extends BaseActionSupport {
	private static final long serialVersionUID = 5901301737793750552L;
		/**
		 * 逻辑处理类
		 */
		IStorageLogic storageLogic;
		IWhLogic whLogic;
		/**
		 * 查询对象
		 */
		StorageQry qry;
		/**
		 * 分页对象
		 **/
		Page page;
		/**
		 * 实体类
		 */
		/**
		 * 商品编码
		 */
		String[] productCodes;
		/**
		 * 入库数量
		 */
		String[] inQtys;
		
		/**
		 * 待入库数量
		 */
		String[] stayInQtys;
		
		/**
		 * 实际价格
		 */
		String[] realPrices;
		
		/**
		 * 
		 */
		Long[] exDIds;
		String billNo;
		List<EmpTWarehouse> whList;
		EmpTWarehouse defaultWh;//默认仓库
		List<StorageCheckDetail> checkDetails;
		StorageCheck check;
		String productCode,whCode;
		/**
		 * 状态 保存1，完结2
		 */
		String flowStatus;
		/**
		 * 列表
		 */
		public String list() throws Exception{
			UserBean userInfo = super.getUserInfo();
		
			if(StringUtils.isNotBlank(super.qryHex)){
				qry = (StorageQry)ObjectUtil.getObjectFromHex(qryHex);
			}
			if(qry==null){
				qry = new StorageQry();
			}
			if(qry.getStartDate() == null)
				qry.setStartDate(DateUtil.getStartDateDay(DateUtils.addDays(DateUtil.getDate(), -30)));
			if(qry.getEndDate() == null)
				qry.setEndDate(DateUtil.getEndDateDay(DateUtil.getDate()));
			qry.setUserInfo(userInfo);
			page = storageLogic.findStorageCheckPage(qry, Page.class);
			return "list";
		}
		/**
		 * 入库确认
		 **/
		public String toCheck() throws Exception{
			UserBean userInfo = super.getUserInfo();
			billNo = qry.getBillNo();
			defaultWh =whLogic.findDefaultWarehouse(userInfo.getDeptId());
			
			if(check == null)
				check = new StorageCheck();
			check.setBillType(qry.getBillType());
			super.title = "入库确认，单号："+billNo;
			checkDetails = storageLogic.findStorageCheckDetailPage(qry);
			whList = whLogic.findWarehouse(userInfo.getDeptId(), null);
			super.title="入库确认";
			return "check";
		}
		public String check() throws Exception{
			UserBean userInfo = super.getUserInfo();
			String str = "";
			if ("CHECK".equals(super.event)) {
				if(check.getBillType().equals("11")){
					EmpTCgM cgm = storageLogic.get(EmpTCgM.class, billNo);
					str = storageLogic.saveStorageCheckCg(userInfo,cgm, check,whCode, exDIds, productCodes, 
							stayInQtys, inQtys, realPrices,flowStatus,
							"11","1",null,cgm.getSaleId());
				}else if(check.getBillType().equals("13")){//
					EmpTWhExM exm = storageLogic.get(EmpTWhExM.class, billNo);
					str = storageLogic.saveStorageCheckEx(userInfo,exm, check,whCode, exDIds, productCodes, 
							stayInQtys, inQtys, flowStatus, "12","1",null,exm.getSalemanId());//12调拨入库
				}
			}
			super.redirectUrl = "/emp/storage_list.action";
			return super.GLOBAL_SUCCESS;
		}
		/**
		 * 查看
		 **/
		public String toView() throws Exception{
			UserBean userInfo = super.getUserInfo();
			//entity = whLogic.get(EmpTWarehouse.class,entity.getWhCode());
			super.title="仓库维护详细信息";
			return "view";
		}
		public IStorageLogic getStorageLogic() {
			return storageLogic;
		}
		public void setStorageLogic(IStorageLogic storageLogic) {
			this.storageLogic = storageLogic;
		}
		public StorageQry getQry() {
			return qry;
		}
		public void setQry(StorageQry qry) {
			this.qry = qry;
		}
		public Page getPage() {
			return page;
		}
		public void setPage(Page page) {
			this.page = page;
		}
		public String getProductCode() {
			return productCode;
		}
		public void setProductCode(String productCode) {
			this.productCode = productCode;
		}
		public String getWhCode() {
			return whCode;
		}
		public void setWhCode(String whCode) {
			this.whCode = whCode;
		}
		public String[] getProductCodes() {
			return productCodes;
		}
		public void setProductCodes(String[] productCodes) {
			this.productCodes = productCodes;
		}
		public String[] getInQtys() {
			return inQtys;
		}
		public void setInQtys(String[] inQtys) {
			this.inQtys = inQtys;
		}
		public String[] getStayInQtys() {
			return stayInQtys;
		}
		public void setStayInQtys(String[] stayInQtys) {
			this.stayInQtys = stayInQtys;
		}
		public String[] getRealPrices() {
			return realPrices;
		}
		public void setRealPrices(String[] realPrices) {
			this.realPrices = realPrices;
		}
		public Long[] getExDIds() {
			return exDIds;
		}
		public void setExDIds(Long[] exDIds) {
			this.exDIds = exDIds;
		}
		public IWhLogic getWhLogic() {
			return whLogic;
		}
		public void setWhLogic(IWhLogic whLogic) {
			this.whLogic = whLogic;
		}
		public StorageCheck getCheck() {
			return check;
		}
		public void setCheck(StorageCheck check) {
			this.check = check;
		}
		public String getBillNo() {
			return billNo;
		}
		public void setBillNo(String billNo) {
			this.billNo = billNo;
		}
		public List<EmpTWarehouse> getWhList() {
			return whList;
		}
		public void setWhList(List<EmpTWarehouse> whList) {
			this.whList = whList;
		}
		public List<StorageCheckDetail> getCheckDetails() {
			return checkDetails;
		}
		public void setCheckDetails(List<StorageCheckDetail> checkDetails) {
			this.checkDetails = checkDetails;
		}
		public EmpTWarehouse getDefaultWh() {
			return defaultWh;
		}
		public void setDefaultWh(EmpTWarehouse defaultWh) {
			this.defaultWh = defaultWh;
		}
		public String getFlowStatus() {
			return flowStatus;
		}
		public void setFlowStatus(String flowStatus) {
			this.flowStatus = flowStatus;
		}
		
}